;$(function(){
    //调整内容高度
    $(function(){
        $(window).resize(function(){
            $('body > .container').css("min-height", $(window).height() - 280);
        }).resize();
    });

    //刷新验证码
    $(function(){
        var verifyimg = $(".verifyimg").attr("src");
        $(".reloadverify").click(function(){
            if( verifyimg.indexOf('?')>0){
                $(".verifyimg").attr("src", verifyimg+'&random='+Math.random());
            }else{
                $(".verifyimg").attr("src", verifyimg.replace(/\?.*$/,'')+'?'+Math.random());
            }
        });
    });

    //ajax get请求
    $('.ajax-get').click(function(){
        var target;
        var that = this;
        if($(this).hasClass('confirm')){
            if(!confirm('确认要执行该操作吗?')){
                return false;
            }
        }
        if((target = $(this).attr('href')) || (target = $(this).attr('url'))){
            $.get(target).success(function(data){
                if(data.status == 1){
                    if (data.url) {
                        swal({
                            type: "success",
                            title: "Good job !",
                            text: data.info+ ' 页面即将自动跳转~',
                            timer: 1000
                        });
                    }else{
                        swal({
                            type: "success",
                            title: "Good job !",
                            text: data.info,
                            timer: 1000
                        });
                    }
                    setTimeout(function(){
                        if (data.url) {
                            location.href = data.url;
                        }else{
                            location.reload();
                        }
                    },1500);
                }else{
                    swal({
                        type: "error",
                        title: "Oops...",
                        text: data.info,
                        timer: 2000
                    });
                    setTimeout(function(){
                        if (data.url) {
                            location.href = data.url;
                        }
                    },2000);
                }
            });
        }
        return false;
    });

    //ajax post submit请求
    $('.ajax-post').click(function(){
        var target,query,form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm=false;
        if(($(this).attr('type')=='submit') || (target = $(this).attr('href')) || (target = $(this).attr('url'))){
            form = $('.'+target_form);
            if($(this).attr('hide-data') === 'true'){ //无数据时也可以使用的功能
                form = $('.hide-data');
                query = form.serialize();
            }else if(form.get(0) == undefined){
                return false;
            }else if(form.get(0).nodeName == 'FORM'){
                if( $(this).hasClass('confirm')){
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                if($(this).attr('url') !== undefined){
                    target = $(this).attr('url');
                }else{
                    target = form.get(0).action;
                }
                query = form.serialize();
            }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA'){
                form.each(function(k,v){
                    if(v.type=='checkbox' && v.checked==true){
                        nead_confirm = true;
                    }
                })
                if(nead_confirm && $(this).hasClass('confirm')){
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                query = form.serialize();
            }else{
                if($(this).hasClass('confirm')){
                    if(!confirm('确认要执行该操作吗?')){
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            $(that).addClass('disabled').attr('autocomplete','off').prop('disabled',true);
            $.post(target,query).success(function(data){
                if(data.status == 1){
                    if(data.url){
                        swal({
                            type: "success",
                            title: "Good job !",
                            text: data.info+ ' 页面即将自动跳转~',
                            timer: 1000
                        });
                    }else{
                        swal({
                            type: "success",
                            title: "Good job !",
                            text: data.info,
                            timer: 1000
                        });
                    }
                    setTimeout(function(){
                        $(that).removeClass('disabled').prop('disabled',false);
                        if (data.url) {
                            location.href = data.url;
                        }else{
                            location.reload();
                        }
                    },1500);
                }else{
                    $(".reloadverify").click();
                    swal({
                        type: "error",
                        title: "Oops...",
                        text: data.info,
                        timer: 2000
                    });
                    setTimeout(function(){
                        $(that).removeClass('disabled').prop('disabled',false);
                        if (data.url) {
                            location.href = data.url;
                        }
                    },2000);
                }
            });
        }
        return false;
    });

    $('.ajax-digg').click(function(){
        var target;
        var that = this;
        var type = $(this).attr('type')
        if ( (target = $(this).attr('href')) || (target = $(this).attr('url')) ) {
            $.get(target).success(function(data){
                if (data.status == 1) {
                    var ret = data.info.split(".");
                    if(ret[0] == "yes"){
                        $(that).addClass('btn-inverse');
                    }else{
                        $(that).removeClass('btn-inverse');
                    }
                    $(that).find('span.num').css({'opacity':'0'}).text(ret[1]).animate({opacity:'1'});
                }else{
                    swal({
                        type: "error",
                        title: "Oops...",
                        text: data.info,
                        timer: 1000
                    });
                }
            });
        }
        return false;
    });

    //Ctrl+Enter键发布
    $('body').keydown(function(event){
        if(event.ctrlKey && event.keyCode == 13){
            $('#submit').click();
            return false;
        }
    });

    //自动隐藏顶部导航
    $(".topnav").autoHidingNavbar();

    //iOS Web APP中点击链接跳转到Safari浏览器新标签页的问题
    if (("standalone" in window.navigator) && window.navigator.standalone) {
        var noddy, remotes = false;
        document.addEventListener('click',
            function(event) {
                noddy = event.target;
                while (noddy.nodeName !== "A" && noddy.nodeName !== "HTML") {
                    noddy = noddy.parentNode;
                }
                if ('href' in noddy && noddy.href.indexOf('http') !== -1 && (noddy.href.indexOf(document.location.host) !== -1 || remotes)) {
                    event.preventDefault();
                    document.location.href = noddy.href;
                }
            },
            false
        );
    }
});
